(ns clj-gallery.routes.buddy
  (:require [compojure.core :refer [defroutes routes context GET POST]]
            [ring.util.response :refer [file-response resource-response]]
            [ring.util.http-response :refer [ok unauthorized]]
            [clj-gallery.utils.auth :refer [restrict-1]]))

(def user 1)

(def rules {:read   true
            :write  true
            :delete false
            :macro  true})

(defn operator-access [op]
  (fn [_]
    (boolean (and user (get rules op)))))

(def read-access-rule {:handler (operator-access :read)})
(def err-handler {:on-error (fn [a b] "aaab")})

(defroutes buddy-routes
  (context "/buddy" []
    (GET "/" [] "buddy")

    (GET "/read" [] (restrict-1 (merge read-access-rule err-handler)
                      "buddy read auth"))
    (GET "/write" [] (restrict-1 {:handler (operator-access :write)}
                       "buddy write auth"))
    (GET "/delete" [] (restrict-1 {:handler (operator-access :delete)}
                        "buddy delete auth"))
    (GET "/macro" [] (restrict-1 {:handler (operator-access :macro)}
                       "buddy with macro"))))
